package com.gzhryc.shared_device.common.device.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.IDeviceService;
import com.gzhryc.shared_device.base.IPlaceService;
import com.gzhryc.shared_device.base.IUserService;
import com.gzhryc.shared_device.base.models.IPlace;
import com.gzhryc.shared_device.base.models.IUser;
import com.gzhryc.shared_device.common.device.DeviceServiceFactory;
import com.gzhryc.shared_device.common.device.dao.DeviceDao;
import com.gzhryc.shared_device.common.device.dao.db.Device;
import com.gzhryc.shared_device.common.device.dao.db.DeviceProduceBatch;
import com.gzhryc.shared_device.common.device.dao.db.DeviceType;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeFunc;
import com.gzhryc.shared_device.common.device.dao.models.DeviceTypeCount;
import com.gzhryc.shared_device.common.device.dao.models.PlaceDeviceCount;
import com.gzhryc.shared_device.common.device.services.dto.DeviceSearch;

public class DeviceService {

    static Logger log = Logger.getLogger(DeviceService.class);

    public static DeviceService self() {
        return MultiDBTools.getService(DeviceService.class, true);
    }

    public static DeviceService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, DeviceService.class, true);
    }

    DeviceDao dao;

    public DeviceService(String jdbcKey) {
        dao = new DeviceDao(jdbcKey);
    }

    public List<Device> findBySearch(DeviceSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.and().in("deviceTypeId",search.getDeviceTypeIds());
        conditions.and().in("placeId",search.getPlaceIds());
        if(search.getDeploy() != null){
            if(search.getDeploy()){
                conditions.and().isNotNull("placeId");
            }else{
                conditions.and().isNull("placeId");
            }
        }
        conditions.asc("deviceSn");

        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(DeviceSearch search){
        Conditions conditions = dao.buildConditions(search);
        conditions.and().in("deviceTypeId",search.getDeviceTypeIds());
        conditions.and().in("placeId",search.getPlaceIds());
        if(search.getDeploy() != null){
            if(search.getDeploy()){
                conditions.and().isNotNull("placeId");
            }else{
                conditions.and().isNull("placeId");
            }
        }

        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public List<String> findDeviceSnBySearch(DeviceSearch search){
        Conditions conditions = dao.buildConditions(search);
        conditions.and().in("deviceTypeId",search.getDeviceTypeIds());
        conditions.and().in("placeId",search.getPlaceIds());
        conditions.and().likeBoth("deviceSn",search.getTrim()).or().likeBoth("placeName",search.getTrim());
        if(search.getDeploy() != null){
            if(search.getDeploy()){
                conditions.and().isNotNull("placeId");
            }else{
                conditions.and().isNull("placeId");
            }
        }

        try {
            return dao.findDeviceSn(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public List<Device> findByPlaceId(Long placeId){
        if(placeId != null) {
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("placeId", placeId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<Device> findByDeviceSnList(List<String> deviceSnList){
        if(deviceSnList != null && deviceSnList.size() > 0){
            Conditions conditions = new Conditions(Device.class);
            conditions.and().in("deviceSn", deviceSnList);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countByAreaCode(String areaCode){
        if(StringTools.isNotBlank(areaCode)){
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("areaCode",areaCode);
            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    public Long countByDeploy(){
        Conditions conditions = new Conditions(Device.class);
        conditions.and().isNotNull("placeId");
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public Long countByPlaceIdAndDeviceTypeId(Long placeId,String deviceTypeId){
        if(placeId != null && StringTools.isNotBlank(deviceTypeId)) {
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("placeId",placeId);
            conditions.and().is("deviceTypeId", deviceTypeId);
            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return 0L;
    }

    public Long countByPlaceId(Long placeId){
        if(placeId != null) {
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("placeId",placeId);
            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return 0L;
    }

    public List<PlaceDeviceCount> findCountByPlaceIds(List<Long> placeIds){
        if(placeIds != null && placeIds.size() > 0) {
            Conditions conditions = new Conditions(Device.class);
            conditions.and().in("placeId",placeIds);

            try {
                return dao.countGroupPlaceId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public List<DeviceTypeCount> countDeviceTypeByPlaceId(Long placeId){
        if(placeId != null){
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("placeId",placeId);

            try {
                return dao.countGroupDeviceTypeId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<PlaceDeviceCount> countPlaceByPlaceIds(List<Long> placeIds){
        if(placeIds != null && placeIds.size() > 0){
            Conditions conditions = new Conditions(Device.class);
            conditions.and().in("placeId",placeIds);

            try {
                return dao.countGroupPlaceId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<PlaceDeviceCount> countPlaceByDeviceTypeIds(Long userId,List<String> deviceTypeIds,Integer state){
        if(deviceTypeIds != null && deviceTypeIds.size() > 0){
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("userId",userId);
            conditions.and().in("deviceTypeId",deviceTypeIds);
            conditions.and().is("state",state);
            conditions.and().isNotNull("placeId");

            try {
                return dao.countGroupPlaceId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Device getByDeviceSn(String deviceSn){
        try {
            return dao.getById(deviceSn);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(Device device) throws LogicException {
        if(StringTools.isBlank(device.getDeviceTypeName())){
            DeviceType deviceType = DeviceTypeService.self().getById(device.getDeviceTypeId());
            if(deviceType == null){
                throw new LogicException("设备类型不存在");
            }
            device.setDeviceTypeName(deviceType.getName());
            device.setNetworkType(deviceType.getNetworkType());
        }

        List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeId(device.getDeviceTypeId());
        if(deviceTypeFuncList != null && deviceTypeFuncList.size() > 0){
            String deviceFunCodes = "";
            String deviceFunNames = "";
            for(DeviceTypeFunc deviceTypeFunc : deviceTypeFuncList) {
                deviceFunCodes = deviceFunCodes + "," + deviceTypeFunc.getCode();
                deviceFunNames = deviceFunNames + "," + deviceTypeFunc.getCodeName();
            }

            deviceFunCodes = deviceFunCodes.substring(1);
            deviceFunNames = deviceFunNames.substring(1);
            device.setDeviceFunCodes(deviceFunCodes);
            device.setDeviceFunNames(deviceFunNames);
        }else{
            throw new LogicException("设备类型没有功能项");
        }

        device.setCreateDate(new Date());
        try {
            return dao.insert(device) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean add(List<Device> deviceList) throws LogicException{
        try {
            return dao.insert(deviceList) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateDeviceTypeName(String deviceTypeId,String deviceTypeName)  {
        if(StringTools.isNotBlank(deviceTypeId) && StringTools.isNotBlank(deviceTypeName)) {
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("deviceTypeId", deviceTypeId);

            Device updateEntity = new Device();
            updateEntity.setDeviceTypeName(deviceTypeName);

            try {
                return dao.updateNotNull(updateEntity, conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean addDeviceFunCode(String deviceTypeId,DeviceTypeFunc deviceTypeFunc){
        if(StringTools.isNotBlank(deviceTypeId) && deviceTypeFunc != null) {
            List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeId(deviceTypeId);
            String deviceFunCodes = deviceTypeFunc.getCode();
            String deviceFunNames = deviceTypeFunc.getNote();

            if(deviceTypeFuncList != null && deviceTypeFuncList.size() > 0) {
                for (DeviceTypeFunc item : deviceTypeFuncList) {
                    if(!item.getCode().equals(deviceTypeFunc.getCode())) {
                        deviceFunCodes = deviceFunCodes + "," + item.getCode();
                        deviceFunNames = deviceFunNames + "," + item.getCodeName();
                    }
                }
            }

            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("deviceTypeId", deviceTypeId);

            Device updateEntity = new Device();
            updateEntity.setDeviceFunCodes(deviceFunCodes);
            updateEntity.setDeviceFunNames(deviceFunNames);

            try {
                return dao.updateNotNull(updateEntity, conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean delDeviceFunCode(String deviceTypeId,String deviceFunCode) throws LogicException {
        if(StringTools.isNotBlank(deviceTypeId) && StringTools.isNotBlank(deviceFunCode)) {
            List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeId(deviceTypeId);
            if(deviceTypeFuncList == null || deviceTypeFuncList.size() <= 0){
                throw new LogicException("设备类型功能项为空");
            }
            String deviceFunCodes = "";
            String deviceFunNames = "";
            for (DeviceTypeFunc item : deviceTypeFuncList) {
                if(!item.getCode().equals(deviceFunCode)) {
                    deviceFunCodes = deviceFunCodes + "," + item.getCode();
                    deviceFunNames = deviceFunNames + "," + item.getCodeName();
                }
            }

            if(deviceFunCodes.length() <= 0 || deviceFunNames.length() <= 0){
                throw new LogicException("设备类型功能项为空");
            }

            deviceFunCodes = deviceFunCodes.substring(1);
            deviceFunNames = deviceFunNames.substring(1);

            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("deviceTypeId", deviceTypeId);

            Device updateEntity = new Device();
            updateEntity.setDeviceFunCodes(deviceFunCodes);
            updateEntity.setDeviceFunNames(deviceFunNames);

            try {
                return dao.updateNotNull(updateEntity, conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean updateNotNull(Device device) throws LogicException{
        List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeId(device.getDeviceTypeId());
        if(deviceTypeFuncList != null && deviceTypeFuncList.size() > 0) {
            String deviceFunCodes = "";
            String deviceFunNames = "";
            for (DeviceTypeFunc item : deviceTypeFuncList) {
                deviceFunCodes = deviceFunCodes + "," + item.getCode();
                deviceFunNames = deviceFunNames + "," + item.getCodeName();
            }
            deviceFunCodes = deviceFunCodes.substring(1);
            deviceFunNames = deviceFunNames.substring(1);

            device.setDeviceFunCodes(deviceFunCodes);
            device.setDeviceFunNames(deviceFunNames);
        }
        try {
            return dao.updateNotNull(device,"user_id","user_name","user_phone","belong_date") > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deploy(Device device, IPlaceService placeService) throws LogicException {
        if (device.getPlaceId() == null || StringTools.isBlank(device.getPlaceName()) || StringTools.isBlank(device.getProvince())
                || StringTools.isBlank(device.getCity()) || StringTools.isBlank(device.getCityArea()) || StringTools.isBlank(device.getAreaCode())) {
            throw new LogicException("请填写部署的网点信息");
        }

        List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeId(device.getDeviceTypeId());
        if (deviceTypeFuncList == null || deviceTypeFuncList.size() == 0) {
            throw new LogicException("设备未配置功能项");
        }

        boolean isSuccess = false;
        JdbcSession.begin();
        try {
            device.setDeployDate(new Date());
            isSuccess = DeviceService.self().updateNotNull(device);
            isSuccess = isSuccess && placeService.deployDevice(device.getPlaceId(),deviceTypeFuncList);
            if (isSuccess) {
                JdbcSession.commit();
                return true;
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        } finally {
            JdbcSession.end();
        }
        return false;
    }
    
    public boolean deploy(String batchId,Integer strategyType,Integer setStartNum,Integer setTotal,
    		Long placeId,IPlaceService placeService,IUserService userService) throws LogicException {
        if (StringTools.isBlank(batchId) || setStartNum == null || strategyType == null || setTotal == null || placeId == null) {
            throw new LogicException("参数错误");
        }
        
        DeviceProduceBatch deviceProductBatch = DeviceProduceBatchService.self().getById(batchId);
        if(deviceProductBatch == null) {
        	throw new LogicException("未找到设备批次信息");
        }

        List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeId(deviceProductBatch.getDeviceTypeId());
        if (deviceTypeFuncList == null || deviceTypeFuncList.size() == 0) {
            throw new LogicException("设备类型未配置功能项");
        }
        
        IPlace place = placeService.getById(placeId);
        if(place == null) {
        	throw new LogicException("未找到网点信息");
        }
        
        IUser user = userService.getById(place.getUserId());
        if(user == null) {
        	throw new LogicException("未找到网点的代理信息");
        }
        
        Device updateEntity = new Device();
		updateEntity.setPlaceId(placeId);
		updateEntity.setPlaceName(place.getName());
		updateEntity.setProvince(place.getProvince());
		updateEntity.setCity(place.getCity());
		updateEntity.setCityArea(place.getCityArea());
		updateEntity.setAreaCode(place.getAreaCode());
		updateEntity.setUserId(user.getId());
		updateEntity.setUserName(user.getName());
		updateEntity.setUserPhone(user.getPhone());
		updateEntity.setDeployDate(new Date());
		
		boolean isSuccess = false;
		
        if(deviceProductBatch.getStartNum().equals(setStartNum) && deviceProductBatch.getProduceTotal().equals(setTotal)) {
        	Conditions condition = null;
        	if(strategyType == 1) {
    			//部署一致代理的设备
        		condition = new Conditions(Device.class);
        		condition.and().is("batchId", deviceProductBatch.getBatchId());
        		condition.and().isNull("placeId");
        		condition.and().is("userId", place.getUserId());
    		}else if(strategyType == 2) {
    			//跳过不一致代理
    			condition = new Conditions(Device.class);
        		condition.and().is("batchId", deviceProductBatch.getBatchId());
        		condition.and().isNull("placeId");
        		condition.and().isNull("userId").or().is("userId", place.getUserId());
    		}else if(strategyType == 3) {
    			//覆盖
    			condition = new Conditions(Device.class);
        		condition.and().is("batchId", deviceProductBatch.getBatchId());
        		condition.and().isNull("placeId");
    		}
        	
        	if(condition != null) {
	            JdbcSession.begin();
	            try {
	                isSuccess = dao.updateNotNull(updateEntity,condition) > 0;
	                isSuccess = isSuccess && placeService.deployDevice(placeId, deviceTypeFuncList);
	                if (isSuccess) {
	                    JdbcSession.commit();
	                }
	            } catch (SQLException e) {
	                log.error(e.getMessage(), e);
	                isSuccess = false;
	            } finally {
	                JdbcSession.end();
	            }
        	}
    	}else {
        	List<String> deviceSnList = DeviceProduceBatchService.self().getDeviceSnList(deviceProductBatch, setStartNum, setTotal);
        	if(deviceSnList != null && deviceSnList.size() > 0) {
        		Conditions condition = null;
            	if(strategyType == 1) {
        			//部署一致代理的设备
            		condition = new Conditions(Device.class);
            		condition.and().in("deviceSn", deviceSnList);
            		condition.and().isNull("placeId");
            		condition.and().is("userId", place.getUserId());
        		}else if(strategyType == 2) {
        			//跳过不一致代理
        			condition = new Conditions(Device.class);
        			condition.and().in("deviceSn", deviceSnList);
            		condition.and().isNull("placeId");
            		condition.and().isNull("userId").or().is("userId", place.getUserId());
        		}else if(strategyType == 3) {
        			//覆盖
        			condition = new Conditions(Device.class);
        			condition.and().in("deviceSn", deviceSnList);
            		condition.and().isNull("placeId");
        		}
            	
            	if(condition != null) {
    	            JdbcSession.begin();
    	            try {
    	                isSuccess = dao.updateNotNull(updateEntity,condition) > 0;
    	                isSuccess = isSuccess && placeService.deployDevice(placeId, deviceTypeFuncList);
    	                if (isSuccess) {
    	                    JdbcSession.commit();
    	                }
    	            } catch (SQLException e) {
    	                log.error(e.getMessage(), e);
    	                isSuccess = false;
    	            } finally {
    	                JdbcSession.end();
    	            }
            	}
        	}
    	}
        
        if(isSuccess) {
        	Conditions condition = new Conditions(Device.class);
        	condition.and().is("batchId", deviceProductBatch.getBatchId());
        	condition.and().is("userId",place.getUserId());
        	condition.and().isNull("belongDate");
        	
        	Device temp = new Device();
        	temp.setBelongDate(new Date());
        	
        	try {
				dao.updateNotNull(temp, condition);
			} catch (SQLException e) {
				 log.error(e.getMessage(), e);
			}
        	return true;
        }
        
        return false;
    }

    public boolean undeploy(Device device, IPlaceService placeService) throws LogicException{
        if(device != null && StringTools.isNotBlank(device.getDeviceSn())) {
            Map<String, Object> data = new HashMap<>();
            data.put("deviceSn", device.getDeviceSn());
            data.put("placeId", null);
            data.put("placeName", null);
            data.put("province",null);
            data.put("city",null);
            data.put("cityArea",null);
            data.put("areaCode",null);
            data.put("bindNote", null);

            boolean isSuccess = false;
            JdbcSession.begin();
            try {
                isSuccess = dao.update(data) > 0;
                isSuccess =  isSuccess && placeService.undeployDevice(device.getPlaceId(),device.getDeviceSn(),device.getDeviceTypeId());
                if (isSuccess) {
                    JdbcSession.commit();
                    return true;
                }
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            } finally {
                JdbcSession.end();
            }
        }
        return false;
    }

    public boolean updateBindNote(String deviceSn,String bindNote){
        if(StringTools.isNotBlank(deviceSn)) {
            Map<String, Object> data = new HashMap<>();
            data.put("deviceSn",deviceSn);
            data.put("bindNote",bindNote);

            try {
                return dao.update(data) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean setBelong(String batchId, IUser user, Integer strategyType){
        if(StringTools.isNotBlank(batchId) && user != null && strategyType != null){
            if(strategyType == 1){
                //跳过
                Conditions conditions = new Conditions(Device.class);
                conditions.and().is("batchId",batchId);
                conditions.and().isNull("userId");
                conditions.and().isNull("placeId");

                Device updateEntity = new Device();
                updateEntity.setUserId(user.getId());
                updateEntity.setUserName(user.getName());
                updateEntity.setUserPhone(user.getPhone());
                updateEntity.setBelongDate(new Date());

                try {
                    if(dao.updateNotNull(updateEntity,conditions) > 0){
                        return true;
                    }
                } catch (SQLException e) {
                    log.error(e.getMessage(),e);
                }
            }else {
                //覆盖
                Conditions conditions = new Conditions(Device.class);
                conditions.and().is("batchId",batchId);
                conditions.and().isNull("placeId");

                Device updateEntity = new Device();
                updateEntity.setUserId(user.getId());
                updateEntity.setUserName(user.getName());
                updateEntity.setUserPhone(user.getPhone());
                updateEntity.setBelongDate(new Date());

                try {
                    if(dao.updateNotNull(updateEntity,conditions) > 0){
                        return true;
                    }
                } catch (SQLException e) {
                    log.error(e.getMessage(),e);
                }
            }
        }
        return false;
    }

    public boolean setBelong(List<String> deviceSnList, IUser user, Integer strategyType){
        if(deviceSnList != null && deviceSnList.size() > 0 && user != null && strategyType != null){
            if(strategyType == 1){
                //跳过
                Conditions conditions = new Conditions(Device.class);
                conditions.and().in("deviceSn", deviceSnList);
                conditions.and().isNull("userId");
                conditions.and().isNull("placeId");

                Device updateEntity = new Device();
                updateEntity.setUserId(user.getId());
                updateEntity.setUserName(user.getName());
                updateEntity.setUserPhone(user.getPhone());
                updateEntity.setBelongDate(new Date());

                try {
                    if(dao.updateNotNull(updateEntity,conditions) > 0){
                        return true;
                    }
                } catch (SQLException e) {
                    log.error(e.getMessage(),e);
                }
            }else {
                //覆盖
                Conditions conditions = new Conditions(Device.class);
                conditions.and().in("deviceSn", deviceSnList);
                conditions.and().isNull("placeId");

                Device updateEntity = new Device();
                updateEntity.setUserId(user.getId());
                updateEntity.setUserName(user.getName());
                updateEntity.setUserPhone(user.getPhone());
                updateEntity.setBelongDate(new Date());

                try {
                    if(dao.updateNotNull(updateEntity,conditions) > 0){
                        return true;
                    }
                } catch (SQLException e) {
                    log.error(e.getMessage(),e);
                }
            }
        }
        return false;
    }
    
    public boolean removeBelong(List<String> deviceSnList){
        if(deviceSnList != null && deviceSnList.size() > 0){
            Conditions conditions = new Conditions(Device.class);
            conditions.and().in("deviceSn", deviceSnList);
            conditions.and().isNull("placeId");
            
            Map<String,Object> data = new HashMap<String,Object>();
            data.put("userId", null);
            data.put("userName", null);
            data.put("userPhone", null);
            data.put("belongDate", null);

            try {
                if(dao.update(data,conditions) > 0){
                    return true;
                }
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
    
    public boolean deleteByBatchId(String batchId) throws LogicException{
        if(StringTools.isNotBlank(batchId)){
            Conditions conditions = new Conditions(Device.class);
            conditions.and().is("batchId",batchId);
            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean deleteByDeviceSn(String deviceSn) throws LogicException{
        Device device = DeviceService.self().getByDeviceSn(deviceSn);
        if(device != null) {
            String[] deviceFunCodes = null;
            if(StringTools.isNotBlank(device.getDeviceFunCodes())){
                deviceFunCodes = device.getDeviceFunCodes().split(",");
            }

            JdbcSession.begin();
            try {
                if(dao.deleteById(deviceSn) > 0){
                    boolean flag = true;
                    if(deviceFunCodes != null && deviceFunCodes.length > 0){
                        for(String funCode : deviceFunCodes){
                            IDeviceService deviceService = DeviceServiceFactory.get(funCode);
                            if(deviceService != null){
                                if(!deviceService.deleteByDeviceSn(deviceSn)){
                                    flag = false;
                                    break;
                                }
                            }else {
                                flag = false;
                                throw new LogicException(funCode + "功能的设备服务未找到");
                            }
                        }
                    }

                    if(flag){
                        JdbcSession.commit();
                        return true;
                    }
                }
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            } finally {
                JdbcSession.end();
            }
        }
        return false;
    }
}
